package com.baichang.taoshunda.controller.mobile.business;

import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.GoodsSpecService;
import com.baichang.taoshunda.service.GoodsSpecValueService;
import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(ControllerConstants.BUSSINESSSPEC)
public class BussinessGoodsSpecMController extends BaseMobileController {

    @Autowired
    private GoodsSpecService goodsSpecService;
    @Autowired
    private GoodsSpecValueService goodsSpecValueService;


    /**
     * 添加规格
     *
     * @param request
     * @param response
     */
    @RequestMapping("/addSpec")
    public void addSpec(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                //是否影响价格  0不影响（添加属性）1影响 添加规格
                Integer isPrice = getInt(json, "isPrice", 0);
                Integer companyId = getInt(json, "companyId", 0);
                Integer goodsId = getInt(json, "goodsId", 0);
                Integer isImage = getInt(json, "isImage", 0);
                //[{"specName":"规格","specValue":[{"value":"大份","price":10}]}]
                String spec = getString(json, "spec", "[]");
                String image = getString(json, "image", "");
                return goodsSpecService.addSpec(isPrice, spec, companyId, goodsId, isImage);
            }
        });
    }

    /**
     * 修改规格
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateSpecValue")
    public void updateSpec(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                //是否影响价格  0不影响（添加属性）1影响 添加规格
                Integer isPrice = getInt(json, "isPrice", 0);
                Integer companyId = getInt(json, "companyId", 0);
                Integer goodsId = getInt(json, "goodsId", 0);
                Integer isImage = getInt(json, "isImage", 0);
                String image = getString(json, "image", "");
                //[{"specName":"规格","specValue":[{"value":"大份","price":10}]}]
                String spec = getString(json, "spec", "[]");
                return goodsSpecService.updateSpecValue(isPrice, companyId, spec, goodsId, isImage);
            }
        });
    }

    /**
     * 删除商品规格
     *
     * @param request
     * @param response
     */
    @RequestMapping("/deleteBySpecId")
    public void deleteBySpecId(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                //是否影响价格  0不影响（添加属性）1影响 添加规格
                Integer specId = getInt(json, "specId", 0);

                return goodsSpecService.deleteBySpecId(specId);
            }
        });
    }

}
